//
//  HMOriginalView.swift
//  WeiBo
//
//  Created by 李立达 on 15/12/10.
//  Copyright © 2015年 heima11. All rights reserved.
//

import UIKit
import SnapKit

let magic : CGFloat = 10

class HMOriginalView: UIView {

    
    var bottomConstraint : Constraint?
    
    var statusModel :HMstatusModel? {
    
        didSet{
        
            iconView.sd_setImageWithURL(NSURL(string: (statusModel?.hmstatus?.user?.profile_image_url)!), placeholderImage: UIImage(named: "avatar_default_big"))
        
            nameLable.text = statusModel?.hmstatus?.user?.name
            timeLable.text = statusModel?.weiboTimeText
            fromLable.text = statusModel?.sourceText
            vipImage.image = statusModel?.vipImage
            verifiedImageView.image = statusModel?.verfiedImage
            textLable.text = statusModel?.hmstatus?.text
            
            self.bottomConstraint?.uninstall()
            if let pic_url = statusModel?.hmstatus?.pic_urls! where pic_url.count > 0
            {
                pictureView.hidden = false
                pictureView.pic_urls = statusModel?.hmstatus?.pic_urls
                
                snp_makeConstraints { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(pictureView).offset(magic).constraint
                }
            }
            else
            {
                pictureView.hidden = true

                snp_makeConstraints { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(textLable).offset(magic).constraint
                }
            }
        }
    }
     //懒加载空间
    //MARK : 头像
    lazy var iconView : UIImageView = {
    let icon = UIImageView(image: UIImage(named: "avatar_default_big"))
        return icon
    }()
    //MARK : 名字
    lazy var nameLable : UILabel = UILabel(color : UIColor.blackColor() ,font: 14)
    //时间
    lazy var timeLable : UILabel = UILabel(color : UIColor.orangeColor() ,font: 10)
    //MARK : laizi
    lazy var fromLable : UILabel = UILabel(color : UIColor.blackColor() ,font: 10)
    //MARK : vip等级图标
    lazy var vipImage : UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //MARK : 认证图标
    lazy var verifiedImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //MARK : 微博内容
    lazy var textLable : UILabel = {
    let lable = UILabel(color: UIColor.grayColor(), font: 14)
    lable.numberOfLines = 0
        lable.preferredMaxLayoutWidth = SCREENW - magic*2
        return lable
    }()
    //MARK : 微博配图
    lazy var pictureView : HMPictureView = {
        let picture = HMPictureView()
        return picture
    }()
    
    //MARK :重写init方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        steupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func steupUI() {
    
    
         addSubview(iconView)
         addSubview(nameLable)
         addSubview(timeLable)
         addSubview(fromLable)
         addSubview(vipImage)
         addSubview(verifiedImageView)
         addSubview(textLable)
         addSubview(pictureView)
        iconView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self).offset(magic)
            make.leading.equalTo(self).offset(magic)
            make.size.equalTo(CGSize(width: 40, height: 40))
        }
        nameLable.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(iconView)
            make.leading.equalTo(iconView.snp_trailing).offset(magic)
        }
     
        vipImage.snp_makeConstraints { (make) -> Void in
            
            make.top.centerY.equalTo(nameLable)
            make.leading.equalTo(nameLable.snp_trailing).offset(magic)
        }
        timeLable.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(iconView)
            make.leading.equalTo(iconView.snp_trailing).offset(magic)
        }
        fromLable.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(timeLable.snp_trailing).offset(magic)
            make.centerY.equalTo(timeLable)
        }
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        
        textLable.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(magic)
            make.top.equalTo(iconView.snp_bottom).offset(magic)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(textLable.snp_bottom).offset(magic)
            make.leading.equalTo(textLable)
        }
        snp_makeConstraints { (make) -> Void in
            self.bottomConstraint = make.bottom.equalTo(pictureView).offset(magic).constraint
        }
    }
    
}
